{% extends "base.html" %}
{% block title %}论坛讨论区 - Online Judge{% endblock %}

{% block content %}
<!-- 背景渐变容器 + 细微纹理 -->
<div class="relative bg-gradient-to-b from-gray-50 to-white py-8 overflow-hidden">
  <!-- 装饰性背景纹理（低透明度） -->
  <div class="absolute inset-0 opacity-5 pointer-events-none">
    <div class="absolute w-64 h-64 rounded-full bg-primary/20 -top-20 -left-20"></div>
    <div class="absolute w-96 h-96 rounded-full bg-primary/10 bottom-10 right-10"></div>
  </div>

  <div class="container mx-auto relative z-10">
    <!-- 顶部栏：标题 + 标语 + 按钮 -->
    <div class="flex flex-col md:flex-row items-start md:items-center justify-between mb-8 gap-4">
      <div>
        <div class="flex items-center space-x-2">
          <i class="fa fa-comments text-2xl text-primary"></i>
          <h1 class="text-3xl font-bold text-gray-800">集市讨论区</h1>
        </div>
        <p class="mt-1 text-gray-500 max-w-md">分享解题思路，交流算法心得，一起攻克编程难题</p>
      </div>

      <div class="flex items-center space-x-3 self-end md:self-auto">
        {% if user.is_authenticated and user.username != 'guest' %}
          <a href="{{ url_for('new_post') }}" class="inline-flex items-center px-4 py-2.5 bg-primary text-white rounded-lg shadow-md hover:bg-primary/90 transition-all duration-200 transform hover:-translate-y-0.5">
            <i class="fa fa-plus mr-2"></i>发布新帖
          </a>
        {% else %}
          <a href="{{ url_for('login', next=url_for('new_post')) }}" class="inline-flex items-center px-4 py-2.5 bg-white text-gray-700 border border-gray-300 rounded-lg shadow-sm hover:bg-gray-50 transition-all duration-200">
            <i class="fa fa-sign-in mr-2"></i>登录发布
          </a>
        {% endif %}
      </div>
    </div>

    <!-- 帖子列表 -->
    {% if posts.items %}
      <div class="grid grid-cols-1 gap-5 md:grid-cols-2 lg:grid-cols-3">
        {% for post in posts.items %}
          <!-- 卡片优化：更细腻的阴影和过渡 -->
          <div class="bg-white rounded-xl shadow-md border border-gray-100 overflow-hidden transition-all duration-300 hover:shadow-lg hover:-translate-y-1">
            <div class="p-5">
              <!-- 标题区域：增强对比 + 置顶/审核标记 -->
              <h2 class="text-xl font-bold text-gray-800 mb-2 leading-tight group">
                <!-- 置顶标记（红色） -->
                {% if post.is_pinned %}
                  <span class="inline-block px-2 py-0.5 bg-red-500 text-white text-xs rounded mr-2">置顶</span>
                {% endif %}
                <!-- 审核标记（绿色=已审核，橙色=待审核-仅root可见） -->
                
                <a href="{{ url_for('forum_post', post_id=post.id) }}" class="hover:text-primary transition-colors duration-200">
                  {{ post.title }}
                </a>
              </h2>

              <!-- 元信息：更清晰的视觉区分 -->
              <!-- 元信息：作者 + 时间 + 审核状态标记 -->
              <div class="text-sm text-gray-500 mb-3 flex flex-wrap items-center gap-x-4 gap-y-1">
                <span class="flex items-center">
                  <i class="fa fa-user-o mr-1.5"></i> {{ post.author.username }}
                </span>
                <span class="flex items-center">
                  <i class="fa fa-clock-o mr-1.5"></i> {{ post.created_at.strftime('%Y-%m-%d') }}
                </span>
                
                <!-- 🔴 审核状态标记：区分 root 和普通用户 -->
                {% if current_user.username == 'root' %}
                  <!-- root 可见所有帖子的审核状态 -->
                  {% if post.is_approved %}
                    <span class="inline-block px-2 py-0.5 bg-green-500 text-white text-xs rounded">
                      已审核
                    </span>
                  {% else %}
                    <span class="inline-block px-2 py-0.5 bg-orange-500 text-white text-xs rounded">
                      待审核
                    </span>
                  {% endif %}
                {% else %}
                  <!-- 普通用户：仅自己的「待审核帖子」显示标记 -->
                  {% if post.user_id == current_user.id and not post.is_approved %}
                    <span class="inline-block px-2 py-0.5 bg-orange-500 text-white text-xs rounded">
                      待审核
                    </span>
                  {% endif %}
                {% endif %}
              </div>


              <!-- 内容摘要：更柔和的颜色 -->
              <p class="text-gray-600 line-clamp-2 mb-4 text-sm leading-relaxed">
                {{ post.content|truncate(80) }}
              </p>

              <!-- 互动信息 + 操作区：编辑 + 置顶按钮 -->
              <div class="pt-3 border-t border-gray-100 flex justify-between items-center">
                <div class="flex items-center gap-4 text-sm text-gray-500">
                  <!-- 点赞图标：替换为 fas fa-thumbs-up -->
                  <span class="flex items-center hover:text-primary transition-colors cursor-pointer">
                    <i class="fas fa-thumbs-up mr-1.5"></i> {{ post.likes|length }}
                  </span>
                  <!-- 评论图标：替换为 fas fa-comment -->
                  <span class="flex items-center hover:text-primary transition-colors cursor-pointer">
                    <i class="fas fa-comment mr-1.5"></i> {{ post.comments|length }}
                  </span>
                </div>

                <div class="flex items-center gap-3">
                  

                  <!-- 置顶/取消置顶按钮（仅 root 可见） -->
                  {% if user.username == 'root' %}
                    <form method="POST" action="{{ url_for('toggle_post_pin', post_id=post.id) }}" class="inline">
                      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />  
                      <button type="submit" class="text-xs font-medium px-2 py-1 rounded hover:underline transition-colors
                        {% if post.is_pinned %}text-red-600 bg-red-50{% else %}text-gray-600 bg-gray-50{% endif %}">
                        {{ '取消置顶' if post.is_pinned else '置顶' }}
                      </button>
                    </form>
                  {% endif %}
                </div>
              </div>
            </div>
          </div>
        {% endfor %}
      </div>

      <!-- 分页导航：增强交互反馈 -->
      <div class="mt-10 flex flex-col sm:flex-row justify-center items-center gap-4">
        <a href="{{ url_for('forum_index', page=posts.prev_num) }}" 
           class="px-4 py-2 bg-white text-gray-600 border border-gray-300 rounded-lg hover:bg-gray-50 transition-all duration-200 {% if not posts.has_prev %}opacity-50 cursor-not-allowed{% endif %}">
          <i class="fa fa-chevron-left mr-1"></i> 上一页
        </a>

        <span class="text-gray-600 whitespace-nowrap">第 {{ posts.page }} 页，共 {{ posts.pages }} 页</span>

        <a href="{{ url_for('forum_index', page=posts.next_num) }}" 
           class="px-4 py-2 bg-white text-gray-600 border border-gray-300 rounded-lg hover:bg-gray-50 transition-all duration-200 {% if not posts.has_next %}opacity-50 cursor-not-allowed{% endif %}">
          下一页 <i class="fa fa-chevron-right ml-1"></i>
        </a>
      </div>

    {% else %}
      <!-- 空状态：更精致的排版和动效 -->
      <div class="flex flex-col items-center justify-center py-16 text-center max-w-2xl mx-auto">
        <img src="https://cdn-icons-png.flaticon.com/512/2211/2211615.png" 
             alt="空状态插画：讨论交流" 
             class="w-40 h-40 md:w-48 md:h-48 mb-6 object-contain opacity-90 transition-transform duration-500 hover:scale-105">
        <h3 class="text-xl md:text-2xl font-semibold text-gray-800 mb-3">还没有任何讨论</h3>
        <p class="text-gray-500 mb-8 leading-relaxed">
          这里等待你的第一个帖子！分享解题技巧、提问困惑，或讨论算法热点，与社区一起成长
        </p>
        <a href="{{ url_for('new_post') }}" class="inline-flex items-center px-6 py-3 bg-primary text-white rounded-lg shadow-md hover:bg-primary/90 transition-all duration-200 transform hover:-translate-y-0.5">
          <i class="fa fa-pencil mr-2"></i>发布第一条帖子
        </a>
      </div>
    {% endif %}
  </div>
</div>
{% endblock %}